import javax.management.ListenerNotFoundException;

/*
 * @lc app=leetcode.cn id=328 lang=java
 *
 * [328] 奇偶链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    /**
     * Accepted
    70/70 cases passed (0 ms)
    Your runtime beats 100 % of java submissions
    Your memory usage beats 99.57 % of java submissions (40.5 MB)
     */
    public ListNode oddEvenList(ListNode head) {
        ListNode cur = head, first = new ListNode(0), second = new ListNode(0), fh = first, sh = second;
        boolean isFirst = true;
        while (cur != null) {
            if (isFirst) {
                first.next = cur;
                first = first.next;
            } else {
                second.next = cur;
                second = second.next;
            }
            cur = cur.next;
            isFirst = !isFirst;
        }
        second.next = null;
        first.next = sh.next;
        return fh.next;
    }
}
// @lc code=end
